﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_ShopData : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    ShopDao sd = new ShopDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup == "管理员")
            {
                dt = sd.getAllShop();
                gvshop.DataSource = dt;
                gvshop.DataBind();
                for (int i = 0; i < gvshop.Rows.Count; i++)
                {
                    string id = dt.Rows[i][0].ToString();
                    id = em.Encript(id);
                    HyperLink hlname = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlname");
                    hlname.NavigateUrl = "ShopInfo.aspx?sid=" + id;
                    HyperLink hlupdate = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlupdate");
                    hlupdate.NavigateUrl = "ShopUpdate.aspx?sid=" + id;
                    HyperLink hlpic = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlpic");
                    hlpic.NavigateUrl = "PicAddUpdate.aspx?sid=" + id;
                }
            }
            else
            {
                Response.Redirect("../index.aspx");
            }
        }
    }
    protected void gvshop_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvshop.PageIndex = e.NewPageIndex;
        dt = sd.getShop("0", "0", "", "0");
        gvshop.DataSource = dt;
        gvshop.DataBind();
        for (int i = 0; i < gvshop.Rows.Count; i++)
        {
            int size = e.NewPageIndex * 20 + i;
            string id = dt.Rows[size][0].ToString();
            id = em.Encript(id);
            HyperLink hlname = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlname");
            hlname.NavigateUrl = "ShopInfo.aspx?sid=" + id;
            HyperLink hlupdate = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlupdate");
            hlupdate.NavigateUrl = "ShopUpdate.aspx?sid=" + id;
            HyperLink hlpic = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlpic");
            hlpic.NavigateUrl = "PicAddUpdate.aspx?sid=" + id;
        }
    }
    protected void gvshop_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "del")
        {
            sd.delShop(id);
        }
        if (e.CommandName == "upd")
        {
            sd.UpdateShopState(id);
        }
        dt = sd.getShop("0", "0", "", "0");
        gvshop.DataSource = dt;
        gvshop.DataBind();
        for (int i = 0; i < gvshop.Rows.Count; i++)
        {
            string sid = dt.Rows[i][0].ToString();
            sid = em.Encript(sid);
            HyperLink hlname = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlname");
            hlname.NavigateUrl = "ShopInfo.aspx?sid=" + sid;
            HyperLink hlupdate = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlupdate");
            hlupdate.NavigateUrl = "ShopUpdate.aspx?sid=" + sid;
            HyperLink hlpic = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlpic");
            hlpic.NavigateUrl = "PicAddUpdate.aspx?sid=" + id;
        }
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        dt = sd.getShop("0", "0", txtname.Text, "0");
        if (dt == null)
        {
            string script = "<script>window.alert('对不起，没有相关店面信息！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        if (dt.Rows.Count == 0)
        {
            string script = "<script>window.alert('对不起，没有相关店面信息！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        gvshop.DataSource = dt;
        gvshop.DataBind();
        for (int i = 0; i < gvshop.Rows.Count; i++)
        {
            string id = dt.Rows[i][0].ToString();
            id = em.Encript(id);
            HyperLink hlname = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlname");
            hlname.NavigateUrl = "ShopInfo.aspx?sid=" + id;
            HyperLink hlupdate = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlupdate");
            hlupdate.NavigateUrl = "ShopUpdate.aspx?sid=" + id;
            HyperLink hlpic = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlpic");
            hlpic.NavigateUrl = "PicAddUpdate.aspx?sid=" + id;
        }
    }
    protected void gvshop_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[9].Attributes.Add("onclick", "return confirm('确认审核通过吗？')");
            e.Row.Cells[10].Attributes.Add("onclick", "return confirm('确认删除吗？')");
        }
    }
    protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    {
        dt = sd.getStateShop(ddlstate.SelectedValue);
        gvshop.DataSource = dt;
        gvshop.DataBind();
        for (int i = 0; i < gvshop.Rows.Count; i++)
        {
            string id = dt.Rows[i][0].ToString();
            id = em.Encript(id);
            HyperLink hlname = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlname");
            hlname.NavigateUrl = "ShopInfo.aspx?sid=" + id;
            HyperLink hlupdate = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlupdate");
            hlupdate.NavigateUrl = "ShopUpdate.aspx?sid=" + id;
            HyperLink hlpic = (HyperLink)gvshop.Rows[i].Cells[0].FindControl("hlpic");
            hlpic.NavigateUrl = "PicAddUpdate.aspx?sid=" + id;
        }
    }
}
